package com.itheima.controller;

import com.itheima.mapper.ResourceMapper;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import jxl.write.WriteException;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/{id}")
    public User  findById(@PathVariable("id") Long id){
        return userService.findById(id);
    }
    @GetMapping("/findPage")
    public List<User>  findPage(
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "10") Integer pageSize){
        return userService.findPage(page,pageSize);
    }
    @GetMapping(value="/downLoadXlsByJxl",name="使用JXL下载")
    public void downLoadXlsByJxl(HttpServletResponse response) throws IOException, WriteException {
        userService.downLoadXlsByJxl(response);
    }
    @PostMapping(value = "/uploadExcel",name="poi导入用户数据")  // file字段不是随意地，而是跟文件上传form data对应
    public void uploadExcel(MultipartFile file) throws Exception {
        userService.uploadExcel(file);
    }

    @GetMapping(value = "/downLoadXlsxByPoi",name="poi导出用户数据（高版本）")  // file字段不是随意地，而是跟文件上传form data对应
    public void downLoadXlsxByPoi(HttpServletResponse response) throws Exception {
//        userService.downLoadXlsxByPoi(response);
//        userService.downLoadXlsxByPoiWithCellStyle(response);
        userService.downLoadXlsxByPoiByTemplate(response);
    }

    @GetMapping(value = "/download",name="poi导出用户详情数据（高版本）")  // file字段不是随意地，而是跟文件上传form data对应
    public void downLoadUserInfoWithTempalte(Long id,HttpServletResponse response) throws Exception {
//        userService.downLoadUserInfoWithTempalte(id,response);
        userService.downLoadUserInfoWithEignee(id,response); // 使用引擎
    }

    @GetMapping(value = "/downLoadMillion",name = "导出用户百万数据的导出")
    public void downLoadMillion(HttpServletResponse response) throws Exception{
        userService.downLoadMillion(response);
    }

    @GetMapping(value = "/downLoadCSV",name = "导出用户数据到CSV文件中")
    public void downLoadCSV(HttpServletResponse response){
        userService.downLoadCSV(response);
    }

    @GetMapping(value = "/downloadContract",name = "下载合同（word）")
    public void downloadContract(Long id,HttpServletResponse response) throws Exception {
        userService.downloadContract(id,response);
    }

    @GetMapping(value = "/downLoadWithEasyPOI",name = "使用EasyPOI下载Excel")
    public void downLoadWithEasyPOI(HttpServletRequest request,HttpServletResponse response) throws Exception{
        userService.downLoadXlsxWithEayPoi(request,response);
    }
}
